package com.ruoyix.ai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.ai.domain.bo.AiApiKeyBo;
import com.ruoyix.ai.domain.vo.AiApiKeyVo;
import com.ruoyix.ai.domain.AiApiKey;
import com.ruoyix.ai.mapper.AiApiKeyMapper;
import com.ruoyix.ai.service.IAiApiKeyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * AI API 密钥Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-22
 */
@RequiredArgsConstructor
@Service
public class AiApiKeyServiceImpl implements IAiApiKeyService {

    private final AiApiKeyMapper baseMapper;

    /**
     * 查询AI API 密钥
     */
    @Override
    public AiApiKeyVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询AI API 密钥列表
     */
    @Override
    public TableDataInfo<AiApiKeyVo> queryPageList(AiApiKeyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AiApiKey> lqw = buildQueryWrapper(bo);
        Page<AiApiKeyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询AI API 密钥列表
     */
    @Override
    public List<AiApiKeyVo> queryList(AiApiKeyBo bo) {
        LambdaQueryWrapper<AiApiKey> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AiApiKey> buildQueryWrapper(AiApiKeyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AiApiKey> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), AiApiKey::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getApiKey()), AiApiKey::getApiKey, bo.getApiKey());
        lqw.eq(StringUtils.isNotBlank(bo.getPlatform()), AiApiKey::getPlatform, bo.getPlatform());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), AiApiKey::getUrl, bo.getUrl());
        lqw.eq(bo.getStatus() != null, AiApiKey::getStatus, bo.getStatus());
        lqw.eq(bo.getDeleted() != null, AiApiKey::getDeleted, bo.getDeleted());
        return lqw;
    }

    /**
     * 新增AI API 密钥
     */
    @Override
    public Boolean insertByBo(AiApiKeyBo bo) {
        AiApiKey add = BeanUtil.toBean(bo, AiApiKey.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改AI API 密钥
     */
    @Override
    public Boolean updateByBo(AiApiKeyBo bo) {
        AiApiKey update = BeanUtil.toBean(bo, AiApiKey.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AiApiKey entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除AI API 密钥
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
